<template>
  <div class="update-name">
    <van-nav-bar
      title="修改昵称"
      left-text="取消"
      right-text="完成"
      @click-left="$emit('close')"
      @click-right="onConfirm"
    >
      <div class="name-field-wrap">
        <van-field
          v-model="localName"
          rows="2"
          autosize
          type="textarea"
          maxlength="50"
          placeholder="请输入昵称"
          show-word-limit
        />
      </div>
    </van-nav-bar>
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user.js'
export default {
  name: 'UpdateName',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  components: {},
  data () {
    return {
      localName: this.value
    }
  },
  computed: {},
  watch: {},
  methods: {
    async onConfirm () {
      try {
        await updateUserProfile({
          name: this.localName
        })
        this.$toast.success('修改昵称成功')
        this.$emit('input', this.localName)
        this.$emit('close')
      } catch (err) {
        if (err && err.response && err.response.status === 409) {
          this.$toast.fail('昵称已存在')
        }
      }
    }
  },
  created () {
  },
  mounted () {
  },
  destroyed () {
  }
}
</script>

<style scoped lang='less'>
.name-field-wrap {
  padding: 10px;
}
</style>
